<!--手动结算-->
<template>
  <div class="main">
    <div class="info-container" v-loading="loading">
      <div class="info-container_body">
        <el-form ref="refFrom" label-position="top" class="demo">
          <el-form-item label="商户名称">
            <el-select v-model="item.vendorId" filterable placeholder="商户名称" @change="getLast()">
              <el-option v-for="item in data.vendors" :key="item.vendorId" :label="item.name" :value="item.vendorId"></el-option>
            </el-select>
          </el-form-item>
          <!-- :disabled-date="disabledDate" -->
          <el-form-item label="结算开始时间">
            <el-date-picker
              v-model="item.timeFrom"
              :disabled="timeFromDisabled"
              type="datetime"
              format=" YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="结算结束时间">
            <el-date-picker
              v-model="item.timeTo"
              type="datetime"
              format=" YYYY-MM-DD HH:mm:ss"
              :disabled-date="disabledDate"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="结算内容">
            <el-checkbox-group v-model="item.settleContents">
              <el-checkbox
                v-for="item in dict.settle_contents"
                :label="item.value"
                :key="item.value"
                :disabledDate="disabledDate "
                :value="item.value"
                >{{ item.label }}</el-checkbox
              >
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </div>
      <div class="info-container_footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSave">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { vendorSettlement, vendorLastSettlement } from '@/api/operation/vendorSettlement/index';
import common from '@/utils/common.ts';

export default {
  computed: {
    times: {
      get() {
        return [this.item.timeFrom, this.item.timeTo];
      },
      set(value) {
        this.item.timeFrom = value[0];
        this.item.timeTo = value[1].replace('00:00:00', '12:00:00');
      }
    }
  },
  props: {
    data: {}
  },
  data() {
    return {
      loading: false,
      proxy: null,
      timeFromDisabled: false,
      item: {
        vendorId: '',
        timeFrom: '2024-06-01 12:00:00',
        timeTo: common.dateFormat(new Date()).substring(0, 10) + ' 12:00:00',
        settleContents: []
      },
      dict: {}
    };
  },
  mounted() {
    const { proxy } = getCurrentInstance() as ComponentInternalInstance;
    this.proxy = proxy;
    this.dict = toRefs<any>(this.proxy?.useDict('settle_contents'));

    this.init();
  },
  methods: {
    init() {
      //this.item = JSON.parse(JSON.stringify(this.data));
    },

    /**
     * 查询最近结算时间
     */
    getLast() {
      this.loading = true;
      vendorLastSettlement(this.item.vendorId).then((res) => {
        this.loading = false;
        if (res.data != null) {
          this.item.timeFrom = res.data.timeTo;
          this.timeFromDisabled = true;
        } else {
          this.timeFromDisabled = false;
        }

        console.log(this.item);
      });
    },

    //时间限制
    disabledDate(time) {
      return time.getTime() > new Date() || time.getTime() < Date.parse(new Date(this.item.timeFrom));
    },

    handleSave() {
      let data = this.item;

      if (data.settleContents.length == 0) {
        ElMessage.warning('结算内容不能为空');
        return;
      }
      if (data.timeFrom == null) {
        ElMessage.warning('结算开始时间不能为空');
        return;
      }

      if (data.timeTo == null) {
        ElMessage.warning('结算结束时间不能为空');
        return;
      }
      if (data.vendorId == '') {
        ElMessage.warning('结算商户不能为空');
        return;
      }
      this.loading = true;
      vendorSettlement(data).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          //todo:
          this.$emit('hideRight');
        } else {
          ElMessage.error(res.msg);
        }
      });
    },
    handleCancel() {
      this.$emit('hideRight');
    },
    /**
     * 匹配结果
     */
    marchType(value, types) {
      for (let i = 0; i < types.length; i++) {
        let type = types[i];
        if (type.value == value) return type.label;
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.main {
  height: 100%;

  .contentFrm {
    padding: 10px;
    background: #f6f7fb;
  }

  .el-input-number__decrease,
  .el-input-number__increase {
    display: none;
  }

  .el-input-number {
    width: 100%;
  }
}
</style>
